package com.example.projectmobile;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AddActivity extends Activity implements LocationListener {

	private LocationManager locationManager;
	EditText name;
	EditText des;
	private String provider;
	private PlaceDataSource datasrc;
	private Place placeData;
	double lat;
	double lon;
	int CAMERA_PIC_REQUEST = 1337;
	boolean takePic = false;
	private Button savebutton;
	private String picname; 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add);
		datasrc = new PlaceDataSource(this); //create db if not exist

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.add, menu);
		return true;
	}

	@SuppressLint("SimpleDateFormat")
	public void onClickSave(View view) { // onclick button save
		if (takePic) { //photo already taken
			name = (EditText) findViewById(R.id.editText1);
			des = (EditText) findViewById(R.id.editText2);
			if (name.getText().toString().length() == 0) {//check required input
				name.setError("Name is required!");
			} else if (des.getText().toString().length() == 0) {//check required input
				des.setError("Description is required!");
			} else {
				locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

				Criteria criteria = new Criteria();
				provider = locationManager.getBestProvider(criteria, false);

				Location location = locationManager
						.getLastKnownLocation(provider);

				if (location != null) {

					System.out.println("Provider " + provider
							+ " has been selected.");

					onLocationChanged(location);

				} else {
					System.out.println("location == null");
					lat = 15;
					lon = 99;
				}

				datasrc.open();
				datasrc.createRecord(name.getText().toString(), des.getText()
						.toString(), lat, lon, picname); //Insert name description lat lon picturepath to DB

				Intent intent = new Intent(AddActivity.this, MainActivity.class);
				startActivity(intent);//return to MainActivity
			}
			
		}else{//take photo
			
			Intent cameraIntent = new Intent(
					android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());//pic name

			File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");//pic path
			imagesFolder.mkdirs();
		
			File image = new File(imagesFolder, "QR_" + timeStamp + ".png");
			picname="QR_" + timeStamp + ".png";
			Uri uriSavedImage = Uri.fromFile(image);

			cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);//set output path
			startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);//start camera
			takePic=true;
			savebutton = (Button) findViewById(R.id.save);
			savebutton.setText("Save");
		}
	}

	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		lat = location.getLatitude();
		lon = location.getLongitude();
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 1337) {

		} else {
			Toast.makeText(AddActivity.this, "Picture NOt taken",
					Toast.LENGTH_LONG);
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

}
